{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import serial\n",
    "from time import time, sleep"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ser = serial.Serial('/dev/ttyUSB0',9600)\n",
    "print(ser.name) \n",
    "\n",
    "# Read from serial moniter\n",
    "line = ser.read_until('e')\n",
    "\n",
    "# Delete ecg_signal.txt file if it's already present.\n",
    "cwd = os.getcwd()\n",
    "check = cwd + \"/ecg_signal.txt\"\n",
    "\n",
    "files = sorted(glob.glob(cwd + '/*.csv'), key=numericalSort)\n",
    "if check in files:\n",
    "    os.remove(check)\n",
    "\n",
    "# Write the data to a file\n",
    "f = open('ecg_signal.txt', 'a')\n",
    "f.write(str(line)[10:])\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Convert .txt file into .mat file for prediction\n",
    "a = np.loadtxt('ecg_signal.txt', dtype = 'object')\n",
    "a1 = a[100:9100]\n",
    "a1 = np.asarray(a1, dtype = 'float64')\n",
    "a1 = a1.reshape(1,-1)\n",
    "scipy.io.savemat('ecg_signal.mat', {'val': a1})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "!python ecg/ecg/predict.py val.json 0.434-0.864-012-0.309-0.892.hdf5"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
